package com.antd.admin.server.service.impl;

import com.antd.admin.api.dto.RoleMenuDTO;
import com.antd.admin.api.entity.SysRoleMenu;
import com.antd.admin.server.mapper.SysRoleMenuMapper;
import com.antd.admin.server.service.ISysRoleMenuService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色菜单表 服务实现类
 * </p>
 *
 * @author 子丶沫
 * @since 2020-11-18
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRolePermission(RoleMenuDTO roleMenuDTO) {
        int delete = baseMapper.delete(Wrappers.<SysRoleMenu>update().lambda().eq(SysRoleMenu::getRoleId, roleMenuDTO.getRoleId()));
        List<SysRoleMenu> roleMenuList = roleMenuDTO.getPermissionIds().stream()
                .map(menuId -> {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setRoleId(roleMenuDTO.getRoleId());
                    roleMenu.setMenuId(menuId);
                    return roleMenu;
                }).collect(Collectors.toList());
        saveBatch(roleMenuList);
        return delete>0;
    }

    @Override
    public List<String> queryRolePermission(String roleId) {
        return baseMapper.selectList(Wrappers.<SysRoleMenu>query()
                .lambda()
                .eq(SysRoleMenu::getRoleId, roleId))
                .stream()
                .map(SysRoleMenu::getMenuId)
                .collect(Collectors.toList());
    }
}
